<?php

namespace App\Http\Controllers\Install;

use App\Admin as AdminModel;
use App\CheckEnvironment;
use App\Http\Controllers\Controller;
use \Artisan;
use \Request;
use \Session;

class Index extends Controller
{
    //

    public function step1(CheckEnvironment $checkEnvironment)
    {
        //环境检测
        $envInfo = $checkEnvironment->checkEnv();
        //目录文件读写检测
        // IS_WRITE
        $dirFileInfo = $checkEnvironment->checkDirFile();

        //函数及扩展库检测
        $funcAndExtInfo = $checkEnvironment->checkFuncAndExt();

        //判断总体环境是否通过
        $adopt = $checkEnvironment->getAdopt();
        Session::flash('adopt', $adopt);
        return view('install.index.step1', [
            'adopt'          => $adopt,
            'envInfo'        => $envInfo,
            'funcAndExtInfo' => $funcAndExtInfo,
            'dirFileInfo'    => $dirFileInfo,
        ]);
    }

    public function step2()
    {
        $adopt = Session::get('adopt');
        if (!$adopt) {
            return redirect('install/step1');
        }
        Session::flash('adopt', $adopt);
        return view('install.index.step2');
    }

    public function step3()
    {
        try {
            //-------------------判断是否通过检测--------------------
            $adopt = Session::get('adopt');
            if (!$adopt) {
                return showError('检测不通过！');
            }

            //-------------------修改配置文件------------------------
            $data    = Request::all();
            $envData = [
                'DB_CONNECTION' => $data['DB_CONNECTION'],
                'DB_HOST'       => $data['DB_HOST'],
                'DB_DATABASE'   => $data['DB_DATABASE'],
                'DB_USERNAME'   => $data['DB_USERNAME'],
                'DB_PASSWORD'   => $data['DB_PASSWORD'],
            ];
            modifyEnv($envData);

            //---------------------创建库-----------------------------
            // 创建连接
            $conn = new \mysqli($data['DB_HOST'], $data['DB_USERNAME'], $data['DB_PASSWORD']);
            // 检测连接
            if ($conn->connect_error) {
                return showError('数据库连接失败');
            }
            $sql = 'CREATE DATABASE IF NOT EXISTS `' . $data['DB_DATABASE'] . '` DEFAULT CHARACTER SET utf8';
            if ($conn->query($sql) !== true) {
                return showError('创建数据表错误: ' . $conn->error);
            }
            $conn->close(); //关闭连接
            Session::flash('env', true);
            return showSuccess();
        } catch (\Exception $e) {
            return showError();
        }
    }

    //进行数据库迁移
    public function step4()
    {

        try {
            $env = Session::get('env');
            if (!$env) {
                return redirect('install/step5')->withErrors('创建数据库失败！');
            }
            //------------------运行数据库迁移--------------------------
            Artisan::call('migrate');
            $result = AdminModel::create([
                'account'        => 'admin',
                'login_password' => '$2y$10$CudYyG4XHVJMHKa.Jf3LhuqN0qQrkXH31VrjQJXQ8F93wLhI2F7Su',
                'is_enable'      => '1',
            ]);
            Session::flash('migrate', true);
            return redirect('install/step5');
        } catch (\Exception $e) {
            return redirect('install/step5')->withErrors('安装失败！');
        }

    }

    public function step5()
    {
        $migrate = Session::get('migrate');
        if ($migrate) {
            modifyEnv(['IS_INSTALL' => 1]);
        }
        return view('install.index.step5');
    }

}
